package com.company;

import java.util.*;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


class Main2 extends JFrame{
    JLabel jlde;
    private Image image; //获取的图像
    private Toolkit toolKit; //Toolkit对象,用于获取图像
    private Socket sock;
    private InputStream reader;
    private OutputStream writer;
    private boolean booxie;
    private DataInputStream dis;
    JLabel zt;
    int sizex=0;
    public Main2(){//构造函数
        super("远程拍照获取实现——swc010");//标题显示内容
        this.setSize(600, 400);//页面大小
        this.setResizable(false);//不可缩放
        this.setLocationRelativeTo(null);//全屏居中居中显示
        jlde=new JLabel();
        jlde.setBounds(0, 0,this.getWidth(), this.getWidth());
        this.add(jlde);
        JButton qd=new JButton("第一步：启动");
        JButton cs=new JButton("第二步：测试");
        JButton hq=new JButton("第三步：拍照");
        JPanel jp=new JPanel();
        zt = new JLabel();
        jp.add(qd);
        jp.add(cs);
        jp.add(hq);
        jp.add(zt);
        Imgben("lala.png");//初始图片显示
        this.setIconImage(new ImageIcon("lala.png").getImage());//图标图片文件
        this.add(jp,"South");
        this.setVisible(true);//显示界面
        qd.addActionListener(new ActionListener(){//-------------------启动按钮监听，开启socket
            @Override
            public void actionPerformed(ActionEvent e) {
                zt.setText("正在启动.......");
                try {


                    sock=new Socket("39.74.145.109",8888);
                    writer=sock.getOutputStream();//获取输出流
                    reader=sock.getInputStream();//获取输入流
                    dis = new DataInputStream(sock.getInputStream());//包装输入流， 允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。
                    writer.write(("0").getBytes("GBK"));//发送确认信息
                    reads();//读数据


                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }});
        cs.addActionListener(new ActionListener(){//------------------------测试是否连接成功

            @Override
            public void actionPerformed(ActionEvent e) {
                try {

                    writer.write(("0").getBytes("GBK"));
                    reads();

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }});
        hq.addActionListener(new ActionListener(){//-------------------获取图片按钮，点击可显示实时图片
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    zt.setText("开始获取......");
                    writer.write(("1").getBytes("GBK"));//发送获取图片指令
                    reads();
                    reads();
                    writer.write(("3").getBytes("GBK"));//发送空的确认获取指令
                    String str= reads();
                    sizex=Integer.parseInt(str.substring(str.indexOf("e")+2));//得到图片大小
                    reads();//读取图片数据
                    zt.setText("  成功获取 *-*  ");
                    Imgben("new.jpg");//显示获取的图片

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }});
        this.addWindowListener(new WindowAdapter() {//点击叉监听，完全退出程序
            public void windowClosing(WindowEvent arg0) {
                System.exit(0);
            }
        });}
    public String reads() throws FileNotFoundException{
        String strread="";
        FileOutputStream fos=null;
        if(sizex>0){//【 开始接收文件 】
            fos = new FileOutputStream("new.jpg");
            byte[] bytes = new byte[1024];
            int length = 0;
            int bensizej=0;
            try {
                while(( length = dis.read(bytes, 0, bytes.length)) != -1) {
                    bensizej+=length;
                    try {
                        fos.write(bytes, 0, length);//以追加的方式写数据
                        fos.flush();
                        if(sizex<=bensizej) break;//获取完成
                    } catch (IOException e) {
                        e.printStackTrace();
                    }  }} catch (IOException e) {
                e.printStackTrace();}
            sizex=0;
            bensizej=0;  //清零计数
        }else{ //【 开始接收普通数据 】
            byte[] bu=new byte[1024];
            try {
                reader.read(bu);
                strread=(new String(bu,"GBK")).trim();
                System.out.println(strread);
                zt.setText(strread);
            } catch (Exception e) {
                e.printStackTrace();
            }}
        return strread;//返回读取的普通数据
    }

    //加载本地图片
    public void Imgben(String imagePath){
        toolKit = Toolkit.getDefaultToolkit();
        repaint(); //重绘屏幕
        image= toolKit.getImage(imagePath);
    }
    public void paint(Graphics g){
        super.paint(g);
        if (image!=null){
            image.getScaledInstance(500, 500, Image.SCALE_DEFAULT);
            ImageIcon ico=new ImageIcon(image);
            ico.setImage(ico.getImage().getScaledInstance(600,370, Image.SCALE_DEFAULT));
            jlde.setIcon(ico);
        }}}

public class Main {
    public static void main(String[] args) {
        new Main2();
    }

}